<!-- 病人列表 -->
<template>
  <div class="container">
    <!-- 页面标题和操作按钮 -->
    <div class="header">
      <h1>研究对象列表</h1>
      <div class="header-actions">
        <el-button type="primary" icon="el-icon-printer">打印</el-button>
        <el-button icon="el-icon-back" @click="back">返回</el-button>
      </div>
    </div>

    <!-- 搜索区域 -->
    <div class="search-box">
      <el-input
        v-model="searchKeyword"
        placeholder="输入编号、姓名搜索研究对象"
        class="search-input"
        prefix-icon="el-icon-search"
        clearable
        @clear="handleSearch"
        @keyup.enter.native="handleSearch"
      />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table v-loading="loading" class="table" :data="tableData" stripe border style="width: 90%">
      <el-table-column prop="selectNumber" label="筛选号" />
      <el-table-column prop="number" label="编号">
        <template slot-scope="scope">
          <el-button type="text" @click="toUser(scope.row)">{{ scope.row.number }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="gender" label="性别">
        <template slot-scope="scope">
          <el-tag :type="scope.row.gender === '男' ? 'primary' : 'danger'" size="small">
            {{ scope.row.gender || '未知' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="joinDate" label="入组日期" />
      <el-table-column prop="selectDate" label="筛选期" />
      <el-table-column label="操作" width="500">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-picture">影像学检查</el-button>
          <el-button type="text" icon="el-icon-office-building">入院信息</el-button>
          <el-button type="text" icon="el-icon-first-aid-kit">住院治疗</el-button>
          <el-button type="text" icon="el-icon-ship">出院信息</el-button>
          <el-button type="text" icon="el-icon-refresh">出院随访(可重复)</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页和页面信息 -->
    <div class="pagination-container">
      <div class="pagination-info">
        显示第 {{ (currentPage - 1) * pageSize + 1 }} 至 {{ Math.min(currentPage * pageSize, total) }} 项结果，共 {{ total }} 项
      </div>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { researchObject } from '@/api/menu'
export default {
  name: 'Home',
  data() {
    return {
      searchKeyword: '',
      tableData: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 90,
      projectId: null
    }
  },
  mounted() {
    this.projectId = localStorage.getItem('CURRENT_PROJECT_ID')
    this.search()
  },
  methods: {
    search() {
      researchObject({
        projectId: this.projectId,
        current: this.currentPage,
        size: this.pageSize,
        keyword: this.searchKeyword
      }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    },
    handleSearch() {
      this.currentPage = 1
      this.search()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.search()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.search()
    },
    back() {
      this.$router.go(-1)
    },
    toUser(row) {
      this.savePatientToLocal(row)
      this.$router.push({
        path: '/dataEntry/DemographicInformation/GeneralInformation'
      }).catch(() => { })
    },
    // 保存患者信息到本地存储
    savePatientToLocal(row) {
      this.$store.dispatch('patient/setCurrentPatient', row)
        .catch(err => {
          console.error('保存患者到 Vuex 出错:', err)
        })

      try {
        localStorage.setItem('CURRENT_PATIENT', JSON.stringify(row))
      } catch (e) {
        console.warn('localStorage 写入失败:', e)
      }
    }
  }
}
</script>

<style scoped>
.container {
  background: #fff;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
}

.header h1 {
  margin: 0;
  color: #303133;
  font-size: 20px;
  font-weight: 500;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-box {
  margin: 0 auto 20px auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 90%;
}

.search-input {
  width: 300px;
}

.table {
  margin: 0 auto;
}

.pagination-container {
  margin: 20px auto 0;
  width: 90%;
  display: flex;
  justify-content: space-between;
}

.pagination-info {
  color: #606266;
  font-size: 14px;
  line-height: 32px;
}
</style>
